package com.zdsoft.blp.zongbuju.shouye.linliquan.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.gongyong.imageTrans.CircleImage;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLHFActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLLunTanActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;
import com.zdsoft.blp.zongbuju.viewpage.fragMent.MyFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/31.
 */
public class SheQuLunTanAdapter extends BaseAdapter implements NetCallback {
            private List<LinLi>list;
            private LayoutInflater inflater;
            private Context context;
            private Intent intent;
            private int id;
            private LinLi user;
            private ViewHolder holder;
            private boolean iszan;
            private LLLunTanActivity llLunTanActivity;
    public SheQuLunTanAdapter(List<LinLi> list, Context context) {
        this.list = list;
        this.context = context;
        inflater=LayoutInflater.from(context);
    }
//    public SheQuLunTanAdapter(List<LinLi> list, LLLunTanActivity llLunTanActivity) {
//        this.list = list;
//        this.llLunTanActivity = llLunTanActivity;
//        inflater = LayoutInflater.from(llLunTanActivity);
//    }
         @Override
            public int getCount() {
               return list!=null?list.size():0;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                 holder = null;
                if (convertView==null){
                    convertView=inflater.inflate(R.layout.linli_shequluntan_item,null);
                    holder=new ViewHolder();
                    holder.shequluntan_name= (TextView) convertView.findViewById(R.id.shequluntan_name);
                    holder.shequluntan_date= (TextView) convertView.findViewById(R.id.shequluntan_date);
                    holder.shequluntan_title= (TextView) convertView.findViewById(R.id.shequluntan_title);
                    holder.shequluntan_zan= (TextView) convertView.findViewById(R.id.shequluntan_zan);
                    holder.shequluntan_pinglun= (TextView) convertView.findViewById(R.id.shequluntan_pinglun);
                    holder.shequluntan_content= (TextView) convertView.findViewById(R.id.shequluntan_content);
                    holder.shequluntan_iv= (ImageView) convertView.findViewById(R.id.shequluntan_iv);
                    holder.shequluntan_contactPhone= (TextView) convertView.findViewById(R.id.shequluntan_contactPhone);
                    holder.shequluntan_pic= (ImageView) convertView.findViewById(R.id.shequluntan_pic);
                    holder.pinglun= (ImageView) convertView.findViewById(R.id.pinglun);
                    holder.ll_zan= (LinearLayout) convertView.findViewById(R.id.ll_zan);
                    holder.ll_shenhe= (LinearLayout) convertView.findViewById(R.id.ll_shenhe);
                    holder.iv_shenhe= (ImageView) convertView.findViewById(R.id.iv_shenhe);
                    holder.tv_delete= (TextView) convertView.findViewById(R.id.tv_delete);
                    holder.zan= (ImageView) convertView.findViewById(R.id.zan);
                    convertView.setTag(holder);
                }else {
                    holder= (ViewHolder) convertView.getTag();
                }
                 user=list.get(position);
                switch (user.getCode()){
                    case 1://社区论坛
                        id=1;//跳转的标识
                        getIntent(holder,id);//跳转的方法
                        holder.shequluntan_contactPhone.setVisibility(View.GONE);
                        holder.shequluntan_pic.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getDate());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getPingNum()+"");
                        holder.shequluntan_zan.setText(user.getZanNum()+"");
                        Picasso.with(context).load(user.getTou()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        iszan=user.isZan();
                        if (iszan==true){
                            holder.zan.setImageResource(R.drawable.zan2);
                        }else if (iszan==false){
                            holder.zan.setImageResource(R.drawable.zan1);
                        }
                        notifyDataSetChanged();
                        holder.zan.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (iszan==true){
                                    getNet(1);
                                    holder.zan.setImageResource(R.drawable.zan1);
                                    llLunTanActivity.getNet();
                                }else if (iszan==false){
                                    getNet(2);
                                    holder.zan.setImageResource(R.drawable.zan2);
                                    llLunTanActivity.getNet();
                                }
                                notifyDataSetChanged();
                            }
                        });
                    break;
                    case 2://二手市场
                        id=3;//跳转的标识
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getDate());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getPingNum()+"");
                        holder.shequluntan_contactPhone.setText(user.getDianhua());
                        if (!"".equals(user.getContentImage())){
                            Picasso.with(context).load(user.getContentImage()).into(holder.shequluntan_pic);
                        }else {
                            holder.shequluntan_pic.setVisibility(View.GONE);
                        }
                        Picasso.with(context).load(user.getTou()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        getIntent(holder,id);//跳转的方法
                        break;
                    case 3://求职招聘
                        id=5;//跳转的标识
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getDate());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getPingNum()+"");
                        holder.shequluntan_contactPhone.setText(user.getContentImage()+": "+user.getDianhua());
                        holder.shequluntan_pic.setVisibility(View.GONE);
                        Picasso.with(context).load(user.getTou()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        getIntent(holder,id);//跳转的方法
                        break;
                    case 4://寻物招领
                        id=6;//跳转的标识
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getCreateTime());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getReplyNum()+"");
                        holder.shequluntan_contactPhone.setText(user.getContact()+": "+user.getContactPhone());
                        String path=user.getPicPath();
                        if (!"".equals(path)){
                            Picasso.with(context).load(path).into(holder.shequluntan_pic);
                        }else {
                            holder.shequluntan_pic.setVisibility(View.GONE);
                        }
                        Picasso.with(context).load(user.getHeadimgurl()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        getIntent(holder,id);//跳转的方法
                        break;
                    case 5://房屋租售
                        id=7;//跳转的标识
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getCreateTime());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getReplyNum()+"");
                        holder.shequluntan_contactPhone.setText(user.getContact()+": "+user.getContactPhone());
                        if (!"".equals(user.getPicPath())){
                            Picasso.with(context).load(user.getPicPath()).into(holder.shequluntan_pic);
                        }else {
                            holder.shequluntan_pic.setVisibility(View.GONE);
                        }
                        Picasso.with(context).load(user.getHeadimgurl()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        getIntent(holder,id);//跳转的方法
                        break;
                    case 22:
                        holder.shequluntan_pic.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getDate());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getPingNum()+"");
                        holder.shequluntan_zan.setText(user.getZanNum()+"");
                        holder.ll_shenhe.setVisibility(View.VISIBLE);
                        switch (user.getApprovedResult()){
                            case -1:
                                Picasso.with(context).load(R.drawable.shz).into(holder.iv_shenhe);
                                break;
                            case 0:
                                Picasso.with(context).load(R.drawable.shwtg).into(holder.iv_shenhe);
                                break;
                            case 1:
                                Picasso.with(context).load(R.drawable.shtg).into(holder.iv_shenhe);
                                break;
                        }
                        if (!"".equals(user.getTou())){
                            Picasso.with(context).load(user.getTou()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        }
                        break;
                    case 23:
                        id=3;
                        getIntent(holder,id);
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getDate());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getPingNum()+"");
                        if (!"".equals(user.getContentImage())){
                            Picasso.with(context).load(user.getContentImage()).into(holder.shequluntan_pic);
                        }else {
                            holder.shequluntan_pic.setVisibility(View.GONE);
                        }
                        holder.ll_shenhe.setVisibility(View.VISIBLE);
                        switch (user.getApprovedResult()){
                            case -1:
                                Picasso.with(context).load(R.drawable.shz).into(holder.iv_shenhe);
                                break;
                            case 0:
                                Picasso.with(context).load(R.drawable.shwtg).into(holder.iv_shenhe);
                                break;
                            case 1:
                                Picasso.with(context).load(R.drawable.shtg).into(holder.iv_shenhe);
                                break;
                        }
                        if (!"".equals(user.getTou())){
                            Picasso.with(context).load(user.getTou()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        }

                        break;
                    case 32:
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getDate());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getPingNum()+"");
                        holder.shequluntan_contactPhone.setText(user.getContentImage()+": "+user.getDianhua());
                        holder.shequluntan_pic.setVisibility(View.GONE);
                        holder.ll_shenhe.setVisibility(View.VISIBLE);
                        switch (user.getApprovedResult()){
                            case -1:
                                Picasso.with(context).load(R.drawable.shz).into(holder.iv_shenhe);
                                break;
                            case 0:
                                Picasso.with(context).load(R.drawable.shwtg).into(holder.iv_shenhe);
                                break;
                            case 1:
                                Picasso.with(context).load(R.drawable.shtg).into(holder.iv_shenhe);
                                break;
                        }
                        if (!"".equals(user.getTou())){
                            Picasso.with(context).load(user.getTou()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        }
                        break;
                    case 42:
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getCreateTime());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getReplyNum()+"");
                        holder.shequluntan_contactPhone.setText(user.getContact()+": "+user.getContactPhone());
                        String path2=user.getPicPath();
                        if (!"".equals(path2)){
                            Picasso.with(context).load(path2).into(holder.shequluntan_pic);
                        }else {
                            holder.shequluntan_pic.setVisibility(View.GONE);
                        }
                        holder.ll_shenhe.setVisibility(View.VISIBLE);
                        switch (user.getApprovedResult()){
                            case -1:
                                Picasso.with(context).load(R.drawable.shz).into(holder.iv_shenhe);
                                break;
                            case 0:
                                Picasso.with(context).load(R.drawable.shwtg).into(holder.iv_shenhe);
                                break;
                            case 1:
                                Picasso.with(context).load(R.drawable.shtg).into(holder.iv_shenhe);
                                break;
                        }
                        if (!user.getHeadimgurl().equals("")){
                            Picasso.with(context).load(user.getHeadimgurl()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        }
                        break;
                    case 52:
                        holder.ll_zan.setVisibility(View.GONE);
                        holder.shequluntan_name.setText(user.getUsername());
                        holder.shequluntan_date.setText(user.getCreateTime());
                        holder.shequluntan_title.setText(user.getTitle());
                        holder.shequluntan_content.setText(Html.fromHtml(user.getContent()));
                        holder.shequluntan_pinglun.setText(user.getReplyNum()+"");
                        holder.shequluntan_contactPhone.setText(user.getContact()+": "+user.getContactPhone());
                        if (!"".equals(user.getPicPath())){
                            Picasso.with(context).load(user.getPicPath()).into(holder.shequluntan_pic);
                        }else {
                            holder.shequluntan_pic.setVisibility(View.GONE);
                        }
                        holder.ll_shenhe.setVisibility(View.VISIBLE);
                        switch (user.getApprovedResult()){
                            case -1:
                                Picasso.with(context).load(R.drawable.shz).into(holder.iv_shenhe);
                                break;
                            case 0:
                                Picasso.with(context).load(R.drawable.shwtg).into(holder.iv_shenhe);
                                break;
                            case 1:
                                Picasso.with(context).load(R.drawable.shtg).into(holder.iv_shenhe);
                                break;
                        }
                        if (!user.getHeadimgurl().equals("")){
                            Picasso.with(context).load(user.getHeadimgurl()).transform(new CircleImage()).into(holder.shequluntan_iv);
                        }
                        break;

                }

                return convertView;
            }
//解析赞的网络请求
    private void getNet(int i) {
        SharedPreferences sp = llLunTanActivity.getSharedPreferences("info", Context.MODE_PRIVATE);
        String uerrId = sp.getString("userId","");
        Net net=new Net();
        net.nameSpace=API.BBS_NAMESPACE;
        net.url=API.BBS_URL;
        switch (i){
            case 1:
                net.methodName=API.CANCELPRAISE;//cancelPraise
                break;
            case 2:
                net.methodName=API.ADDPRAISE;//addPraise
                break;
        }
        Map<String,Object>map=new HashMap<>();
        map.put("userId",uerrId);
        map.put("id",user.getId());
        net.parms=map;
        llLunTanActivity.getServer(net,SheQuLunTanAdapter.this);
        notifyDataSetChanged();
    }

    private void getIntent(ViewHolder holder, final int id) {
        holder.pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (id){
                    case 1://社区论坛
                        intent=new Intent(context,LLHFActivity.class);
                        intent.putExtra("id","1");
                        break;
                    case 3://二手市场
                    intent=new Intent(context,LLHFActivity.class);
                        intent.putExtra("id","3");
                        break;
                    case 5://求职招聘
                    intent=new Intent(context,LLHFActivity.class);
                        intent.putExtra("id","5");
                        break;
                    case 6://寻物招领
                    intent=new Intent(context,LLHFActivity.class);
                        intent.putExtra("id","6");
                        break;
                    case 7://房屋租售
                    intent=new Intent(context,LLHFActivity.class);
                        intent.putExtra("id","7");
                        break;
                }
                context.startActivity(intent);
            }
        });
    }
//回调函数解析 文件
    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag){
            try {
                JSONObject object = new JSONObject(res);
                if(object.getBoolean("result")){
                    Toast.makeText(llLunTanActivity,object.getString("msg"),Toast.LENGTH_SHORT).show();
                    llLunTanActivity.getNet();
                    notifyDataSetChanged();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    class ViewHolder{
        TextView shequluntan_name,shequluntan_date,
                shequluntan_title,shequluntan_content,
                shequluntan_contactPhone,
                shequluntan_zan,shequluntan_pinglun,tv_delete;
        ImageView shequluntan_iv,shequluntan_pic,iv_shenhe,pinglun,zan;
        LinearLayout ll_zan,ll_shenhe;

    }

    public List<LinLi> setListData(List<LinLi> list){
        return this.list=list;
    }
}

